package org.huntaway.kindergarten.test;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.huntaway.hibernate.SessionManager;
import org.huntaway.hibernate.imp.SessionManagerImp;
import org.huntaway.kernel.NutException;
import org.huntaway.kindergarten.po.FinanceType;
import org.huntaway.kindergarten.po.SysUser;
import org.huntaway.kindergarten.po.base.BasePo;

public class KindergartenInit {
	SessionManager dateManage = null;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		KindergartenInit ki=new KindergartenInit();
		try {
			ki.star();
		} catch (NutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void star() throws NutException {
		initSession();
		insertUser();
		insertFinanceType();
	}

	private void insertUser() {
		String[] kj={"admin","管理员","123"};
		setUser(kj);
		String[] kj1={"kj","会计","123"};
		setUser(kj1);
	}
	private void insertFinanceType() {
		String[] kj={"income","收入","123"};
		setFinanceType(kj);
		String[] kj1={"oayout","支出","123"};
		setUser(kj1);
	}

	private void setFinanceType(String[] kj) {
		FinanceType admin=new FinanceType();
		admin.setTCode(kj[0]);
		admin.setTName(kj[1]);
		admin.setIsuse(1);
		admin.setAuthor("admin");
		admin.setCreateDate(new Date());
		save(admin);
	}

	private void setUser(String[] kj) {
		SysUser admin=new SysUser();
		admin.setTCode(kj[0]);
		admin.setTName(kj[1]);
		admin.setIsuse(1);
		admin.setPassword(kj[2]);
		admin.setAuthor("admin");
		admin.setCreateDate(new Date());
		save(admin);		
	}

	private void save(BasePo admin) {
//		changStr(admin);
		admin.setAuthor("admin");
		admin.setCreateDate(new Date());
		dateManage.save(admin);		
	}
	private void changStr(Object admin) {
		Map<String, Field> fieldMap = new HashMap<String, Field>();
		getAllField(admin.getClass(), fieldMap);
		while(fieldMap.isEmpty()){
		//Field item = fieldMap.size().get(name);
		}
	}

	protected Field getFielld(Class<?> clazz, String name) {
		Map<String, Field> fieldMap = new HashMap<String, Field>();
		getAllField(clazz, fieldMap);
		Field item = fieldMap.get(name);
		return item;
	}
	private void getAllField(Class<?> clazz, Map<String, Field> fieldMap) {
		if (clazz.equals(Object.class)) {
			return;
		}
		Class<?> superClazz = clazz.getSuperclass();
		getAllField(superClazz, fieldMap);
		Field[] allField = clazz.getDeclaredFields();
		for (int i = 0; i < allField.length; i++) {
			Field item = (Field) allField[i];
			fieldMap.put(item.getName(), item);
		}
	}
	private void initSession() throws NutException {
		String path = getWork()+"src/main/resources";
		File cfgFile = new File(path, "hibernate.cfg.xml");
		dateManage = new SessionManagerImp(cfgFile);	

	}
	private static String getHome() {		
		return "e:/jskyme/work/google.com/trunk/org.huntaway/org.huntaway.kindergarten/";
	}
	private static String getWork() {		
		return "E:/jskyme/code/com.google.code/jskyme/trunk/org.huntaway/org.huntaway.kindergarten/";
	}
}
